package com.ruoyi.lab.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lab.domain.LabAppointMessage;
import com.ruoyi.lab.domain.LabSeatOperate;
import com.ruoyi.lab.service.ILabAppointMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lab.mapper.LabSeatMessageMapper;
import com.ruoyi.lab.domain.LabSeatMessage;
import com.ruoyi.lab.service.ILabSeatMessageService;

/**
 * 实验室座位表Service业务层处理
 * 
 * @author dn
 * @date 2023-12-31
 */
@Service
@Slf4j
public class LabSeatMessageServiceImpl implements ILabSeatMessageService 
{
    @Autowired
    private LabSeatMessageMapper labSeatMessageMapper;

    @Autowired
    private ILabAppointMessageService labAppointMessageService;

    /**
     * 查询实验室座位表
     * 
     * @param seatId 实验室座位表主键
     * @return 实验室座位表
     */
    @Override
    public LabSeatMessage selectLabSeatMessageBySeatId(Long seatId)
    {
        return labSeatMessageMapper.selectLabSeatMessageBySeatId(seatId);
    }

    /**
     * 查询实验室座位表列表
     * 
     * @param labSeatMessage 实验室座位表
     * @return 实验室座位表
     */
    @Override
    public List<LabSeatMessage> selectLabSeatMessageList(LabSeatMessage labSeatMessage)
    {
        return labSeatMessageMapper.selectLabSeatMessageList(labSeatMessage);
    }

    /**
     * 新增实验室座位表
     * 
     * @param labSeatMessage 实验室座位表
     * @return 结果
     */
    @Override
    public int insertLabSeatMessage(LabSeatMessage labSeatMessage)
    {
        // 先判断座位名称是否重复
        List<LabSeatMessage> seatMessageList = labSeatMessageMapper.selectLabSeatMessageList(labSeatMessage);
        if (!seatMessageList.isEmpty()) {
            log.error("座位名称重复");
            return -1;
        }

        labSeatMessage.setSeatId(System.currentTimeMillis());
        labSeatMessage.setCreateBy(SecurityUtils.getUsername());
        labSeatMessage.setCreateTime(DateUtils.getNowDate());
        return labSeatMessageMapper.insertLabSeatMessage(labSeatMessage);
    }

    /**
     * 修改实验室座位表
     * 
     * @param labSeatMessage 实验室座位表
     * @return 结果
     */
    @Override
    public int updateLabSeatMessage(LabSeatMessage labSeatMessage)
    {
        Long seatId = labSeatMessage.getSeatId();
        if (seatId == null) {
            log.error("座位id不能为空");
            return -1;
        }

        // 判断修改后的座位名称是否与当前实验室内的座位名称重复
        LabSeatMessage temp = new LabSeatMessage();
        Long labId = labSeatMessage.getLabId();
        if (labId == null) {
            log.error("所属实验室id不能为空");
            return -1;
        }
        temp.setLabId(labId);

        String seatName = labSeatMessage.getSeatName();
        if (seatName == null) {
            log.error("座位名称不能为空");
            return -1;
        }
        temp.setSeatName(seatName);

        List<LabSeatMessage> seatMessageList = labSeatMessageMapper.selectLabSeatMessageList(temp);
        for (LabSeatMessage seatMessage : seatMessageList) {
            if (!seatId.equals(seatMessage.getSeatId())) {
                log.error("座位名称重复");
                return -1;
            }
        }

        labSeatMessage.setUpdateBy(SecurityUtils.getUsername());
        labSeatMessage.setUpdateTime(DateUtils.getNowDate());
        return labSeatMessageMapper.updateLabSeatMessage(labSeatMessage);
    }

    /**
     * 批量删除实验室座位表
     * 
     * @param seatIds 需要删除的实验室座位表主键
     * @return 结果
     */
    @Override
    public int deleteLabSeatMessageBySeatIds(Long[] seatIds)
    {
        // 如果该座位有人预约，则不能删除
        // TODO 后期根据预约期限进行业务优化
        for (Long seatId : seatIds) {
            LabAppointMessage labAppointMessage = new LabAppointMessage();
            labAppointMessage.setSeatId(seatId);
            List<LabAppointMessage> appointMessageList = labAppointMessageService.selectLabAppointMessageList(labAppointMessage);
            if (appointMessageList.isEmpty()) {
                labSeatMessageMapper.deleteLabSeatMessageBySeatId(seatId);
            }
        }
        return 1;
    }

    /**
     * 删除实验室座位表信息
     * 
     * @param seatId 实验室座位表主键
     * @return 结果
     */
    @Override
    public int deleteLabSeatMessageBySeatId(Long seatId)
    {
        return labSeatMessageMapper.deleteLabSeatMessageBySeatId(seatId);
    }
}
